import 'dart:convert';

import 'package:flutter_axrjc/datasource/db/DBLightApi.dart';
import 'package:flutter_axrjc/datasource/network/api/LightApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/EventBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/LampBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/LightBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_axrjc/main.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'state.dart';

class SelectLampDialogLogic extends GetxController {
  final bool showAll;
  final SelectLampDialogState state = SelectLampDialogState();
  var page = 1;
  var refreshController = RefreshController();
  @override
  void onInit() {
    super.onInit();
    if (showAll) {
      state.list.add(
          LampBean(LightBean(fixtureName: "通用", fixtureId: "all"), true.obs));
    }
    DBApi.getAll().then((value) {
      if (value.isNotEmpty) {
        state.list.addAll(value
            .map((e) => LampBean(
                LightBean(
                    lastDetectionTime: e.lastDetectionTime,
                    fixtureId: e.fixtureId,
                    fixtureName: e.fixtureName,
                    fixtureBrand: e.fixtureBrand,
                    fixtureModel: e.fixtureModel,
                    fixtureImg: e.fixtureImg,
                    fixtureType: e.fixtureType,
                    installationDate: e.installationDate,
                    fixtureStatus: e.fixtureStatus,
                    brightnessAdjustment: e.brightnessAdjustment,
                    colorTemperatureAdjustment: e.colorTemperatureAdjustment,
                    remark: e.remark,
                    ifBack: e.ifBack,
                    customerId: e.customerId,
                    sysCode: e.sysCode != null
                        ? SysCode.fromJson(jsonDecode(e.sysCode!))
                        : null),
                false.obs))
            .toList());
      } else {
        refreshController.requestRefresh();
      }
    });
  }

  SelectLampDialogLogic({required this.showAll});
  void addLight() {
    eventBus.fire(EventBean(type: EventType.changeMainPage, data: 1));
    eventBus.fire(EventBean(type: EventType.changeManagePage, data: 0));
    Get.back();
    Get.toNamed(MainRoutes.addLight);
  }

  Future<void> refreshMyLights({required bool isRefresh}) async {
    if (isRefresh) {
      page = 1;
      state.list.clear();
      if (showAll) {
        state.list.add(
            LampBean(LightBean(fixtureName: "通用", fixtureId: "all"), true.obs));
      }
    } else {
      page++;
    }
    try {
      state.list.addAll((await LightApi.getAll(page: page))
          .map((e) => LampBean(e, false.obs))
          .toList());
    } catch (e) {
      if (e is ApiException) {
        EasyLoading.showToast(e.message);
      } else {
        print(e);
      }
    }
    if (isRefresh) {
      refreshController.refreshCompleted();
    } else {
      refreshController.loadComplete();
    }
  }
}
